# codeing=utf-8
import pymysql;


class DbMysql:
    connectionTotal = 0;

    def __init__(self):
        DbMysql.connectionTotal += 1;

    # python以__线开头定义私有方法
    def __openconn(self):
        conn = pymysql.connect(host='localhost', user='root', passwd='', db='ci_test', charset='utf8');
        return conn;

    def query(self, sqlstr, params):
        conn = self.__openconn();
        cursor = conn.cursor(pymysql.cursors.DictCursor);  # 设置游标返回数据为字典格式
        rs = cursor.execute(sqlstr, params);
        if rs > 1:
            results = cursor.fetchall();
        else:
            results = cursor.fetchone();
        conn.commit();
        cursor.close();
        conn.close();
        return results;

    def execute(self, sqlstr, args):
        conn = self.__openconn()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        rs = cursor.execute(sqlstr, args)
        # print(rs)
        conn.commit()
        cursor.close()
        conn.close()
        return rs

    def getConnectionToatl(self):
        return DbMysql.connectionTotal;
